package com.zh.note.controller;

import com.zh.note.entity.JobBean;
import com.zh.note.job.MyJob;
import com.zh.note.utils.JobUtils;
import org.quartz.Scheduler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/quartz")
public class QuartzController {

    @Autowired
    private Scheduler scheduler;

    private static final String JOB_NAME = "myjob";

    @GetMapping("/create")
    public String createJob() {
        JobBean jobBean = new JobBean(JOB_NAME, MyJob.class.getName(), "0/2 * * * * ?");
        JobUtils.createJob(scheduler, jobBean);
        return "创建定时任务成功";
    }

    @GetMapping("/pause")
    public String pauseJob() {
        JobUtils.pauseJob(scheduler, JOB_NAME);
        return "暂停定时任务成功";
    }

    @GetMapping("/resume")
    public String resumeJob() {
        JobUtils.resumeJob(scheduler, JOB_NAME);
        return "暂停定时任务成功";
    }

    @GetMapping("/delete")
    public String deleteJob() {
        JobUtils.deleteJob(scheduler, JOB_NAME);
        return "暂停定时任务成功";
    }

    @GetMapping("/once")
    public String runJobOnceJob() {
        JobUtils.runJobOnce(scheduler, JOB_NAME);
        return "执行一次定时任务成功";
    }

    @GetMapping("/modify")
    public String modifyJob() {
        JobBean jobBean = new JobBean(JOB_NAME, MyJob.class.getName(), "0/5 * * * * ?");
        JobUtils.modifyJob(scheduler, jobBean);
        return "修改定时任务成功";
    }

}
